﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinFormsApp1.utils
{
    public class ColorUtils
    {

        /// <summary>
        /// hex转换到color
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        public static Color HexToColor(string hex)
        {
            byte br = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
            byte bg = byte.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
            byte bb = byte.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);


            Color c = Color.FromArgb(50, br, bg, bb);
            return c;
        }

        public static Color HexToApproximateColor(string hex)
        {
            
            if (hex.Contains("#"))
            {
                hex = hex.Remove(0, 1);
            }
            int br = int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
            int bg = int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
            int bb = int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);

            if (br > 20)
            {

                br = br - 5;
            }

            if (bg > 20)
            {
                bg = bg - 5;
            }
            if (bb > 20)
            {
                bb = bb - 5;
            }

            // MessageBox.Show(br.ToString());
            Color c = Color.FromArgb(br, bg, bb);
            return c;
        }

        public static string ColorToHexValue(Color color)
        {
            return "#" + color.R.ToString("X2") +
                         color.G.ToString("X2") +
                         color.B.ToString("X2");
        }
    }
}
